#include "StdAfx.h"
#include "ListFileMultiMedia.h"
#include "MultiMediaDlg.h"
IMPLEMENT_DYNAMIC(ListFileMultiMedia,CWnd)

ListFileMultiMedia::ListFileMultiMedia(void)
{
}

ListFileMultiMedia::~ListFileMultiMedia(void)
{
}
BEGIN_MESSAGE_MAP(ListFileMultiMedia, CWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_PLAYLIST, OnLvnItemchangedPlaylist)
END_MESSAGE_MAP()

ListFileMultiMedia *gInstance;
ListFileMultiMedia* ListFileMultiMedia::ShareInstance()
{
	return gInstance;
}

int ListFileMultiMedia::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	gInstance = this;
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	  
	m_listControlFile.Create(WS_CHILD | WS_VISIBLE,CRect(0, 0, 0, 0), this, IDC_PLAYLIST);
    InitPlayList();
	m_listControlFile.ShowWindow(SW_SHOW);
	
	m_nNumOfPlaylist = 0;
	m_nOldNumNumOfPlaylist = 0;

	return 0;
}

void ListFileMultiMedia::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
	CWnd *parent = GetParent();
	CRect rect;
	this->GetClientRect(&rect);
	parent->GetClientRect(&rect);
	CRect rtViewArea(0,rect.top,rect.left+200, rect.bottom);
	m_listControlFile.SetWindowPos(parent,0,rtViewArea.top, rtViewArea.left+200, rtViewArea.bottom, SWP_NOZORDER);
	
}
void ListFileMultiMedia::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CWnd::OnPaint() for painting messages
}



BOOL ListFileMultiMedia::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	CRect rect;
	GetClientRect(&rect);
	CBrush myBrush(COLOR_VIEW_AREA);    // dialog background color
	CBrush *pOld = pDC->SelectObject(&myBrush);
	BOOL bRes  = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
	pDC->SelectObject(pOld);    // restore old brush
	return bRes;

	return CWnd::OnEraseBkgnd(pDC);
}
void ListFileMultiMedia::InitPlayList()

{ 
	CRect rect;
	m_listControlFile.GetClientRect(&rect);
    LVCOLUMN lvColumn;
	int nCol;
	m_listControlFile.SetView(LVS_REPORT);
	m_listControlFile.ModifyStyleEx(0, LVS_REPORT , NULL);
	m_listControlFile.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP | LVS_EX_ONECLICKACTIVATE);

	lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
	lvColumn.fmt = LVCFMT_CENTER;
	lvColumn.cx = 200;
	lvColumn.pszText = _T("          Play List");
	nCol = m_listControlFile.InsertColumn(0, &lvColumn);

}
void ListFileMultiMedia::AddDataToPlayList(CListCtrl &ctrl, int row, int col, CString str)
{
    LPWSTR pointer; 
	pointer=(LPWSTR)(LPCTSTR)str; 
	LVITEM lv;
	lv.iItem = row;
	lv.iSubItem = col;
	lv.pszText =  pointer;
	lv.mask = LVIF_TEXT;
	if(col == 0)
		ctrl.InsertItem(&lv);
	else
		ctrl.SetItem(&lv); 

}

void ListFileMultiMedia::OnLvnItemchangedPlaylist(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: Add your control notification handler code here
	
		POSITION p = m_listControlFile.GetFirstSelectedItemPosition();
		while (p)
		{
			int nSelected = m_listControlFile.GetNextSelectedItem(p);
			m_nOldNumNumOfPlaylist = m_nNumOfPlaylist;
			m_nNumOfPlaylist = nSelected;
			if (CMultiMediaDlg::ShareInstance()->m_bIsPlaying)
				CMultiMediaDlg::ShareInstance()->m_VideoMng.MultiMedia_Stop();
			    CMultiMediaDlg::ShareInstance()->MediaPlay();
		}
		if(CMultiMediaDlg::ShareInstance()->m_Audio == true)
	{
	CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_CHOOSEONEVIDEO, MF_DISABLED);
	CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_HIDE, MF_DISABLED);
	CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_CHANGEVIDEO, MF_DISABLED);
	}
	else
	{
		if(CMultiMediaDlg::ShareInstance()->m_bIsPlaying == true && CMultiMediaDlg::ShareInstance()->m_bToggle == false)
		{
			//here
			if(CMultiMediaDlg::ShareInstance()->m_checkHide == true)
			{
			    CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_CHOOSEONEVIDEO, MF_DISABLED);
				CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_HIDE, MF_ENABLED);
				CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_CHANGEVIDEO, MF_DISABLED);
			}
			else
			{
				CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_CHOOSEONEVIDEO, MF_ENABLED);
				CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_HIDE, MF_DISABLED);
				CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_CHANGEVIDEO, MF_DISABLED);
			}
			
		}
		if(CMultiMediaDlg::ShareInstance()->m_bIsPlaying == true && CMultiMediaDlg::ShareInstance()->m_bToggle == true)
		{
		    
			CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_CHOOSEONEVIDEO, MF_ENABLED);
			CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_HIDE, MF_ENABLED);
			CMultiMediaDlg::ShareInstance()->m_Cmenu.EnableMenuItem(ID_SHOWVIDEO_CHANGEVIDEO, MF_ENABLED);
			CMultiMediaDlg::ShareInstance()->m_gControlMedia.m_btnChangeScreen.EnableButton(true);
	        CMultiMediaDlg::ShareInstance()->m_gControlMedia.m_btnChangeScreen.EnableWindow(true);
		}
	}

	*pResult = 0;
}

void ListFileMultiMedia::PlayCurrentTrack()
{
	m_listControlFile.SetItemState(m_nOldNumNumOfPlaylist, ~LVIS_SELECTED, LVIS_SELECTED);
	m_listControlFile.SetItemState(m_nNumOfPlaylist,LVIS_SELECTED, LVIS_SELECTED);
	m_listControlFile.SetSelectionMark(m_nNumOfPlaylist);
	m_listControlFile.SetFocus();
}
